package concurrent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by xiao on 2017/12/17 下午10:25.
 */
public class MxQueueBuffer {
    private List<Integer> queue = new ArrayList<>();

    synchronized int get() {
        if (isEmpty())
            try {
                notify(); // 调用过wait方法的线程可以去参与获得锁的竞争，需等待当前方法执行完
                wait();   // 立即让出该同步锁
            } catch (InterruptedException e) {
                System.out.println("InterruptedException caught");
            }
        System.out.println(queue.size());
        return queue.remove(0);
    }

    synchronized void put(Integer[] n) {
        try {
            if (!isEmpty()) {
                wait();
            }
            //Thread.sleep(500);
            queue.addAll(Arrays.asList(n));
            notify();
        } catch (InterruptedException e) {
            System.out.println("InterruptedException caught");
        }
    }

    private boolean isEmpty() {
        return queue.size() == 0;
    }

    private boolean isFull() {
        return queue.size() == 3;
    }
}
